import java.awt.*;
import java.awt.event.KeyEvent;

public class Body {
	private Node hand = null;
	private Node tail = null;
	private int size = 0;
	
	private Node startNode = new Node(24,24,Direction.L);
	public Body(){
		hand = startNode;
		tail = startNode;
		size = 1;
	}
	
	public void addToHand() {
		Node node = null;
		switch(hand.dir){
		case U:
			node = new Node(hand.row, hand.col - 1, hand.dir);
			break;
		case D:
			node = new Node(hand.row, hand.col + 1, hand.dir);
			break;
		case L:
			node = new Node(hand.row -1, hand.col, hand.dir);
			break;
		case R:
			node = new Node(hand.row + 1, hand.col,hand.dir);
			break;
		}
		node.next = hand;
		hand.prev = node;
		hand = node;
		size ++;
	}
	
	public void addToTail() {
		Node node = null;
		switch(tail.dir){
		case U:
			node = new Node(hand.row, hand.col + 1, hand.dir);
			break;
		case D:
			node = new Node(hand.row, hand.col - 1, hand.dir);
			break;
		case L:
			node = new Node(hand.row + 1, hand.col, hand.dir);
			break;
		case R:
			node = new Node(hand.row - 1, hand.col,hand.dir);
			break;
		}
		tail.next = node;
		node.prev = tail;
		tail = node;
		size ++;
	}
	
	public void deleteToTail() {
		tail = tail.prev;
		tail.next = null;
		size --;
	}
	
	public void move() {
		addToHand();
		deleteToTail();
	}
	
	public boolean eat(Food f) {
		if(f.row == hand.row && f.col == hand.col) {
			f.reAppaer();
			addToTail();
			return true;
		}
		return false;
	}
	
	public boolean die() {
		if(hand.row >= Yard.ROWS || hand.row < 0 || hand.col >= Yard.COLS || hand.col < 0) {
			return false;
		}
		for(Node n = hand.next; n != null; n = n.next){
			if(hand.row == n.row && hand.col == n.col){
				return false;
			}
		}
		return true;
	}
	
	public int getSize() {
		return size;
	}
	
	public void draw(Graphics g) {
		move();
		if(size <= 0) return;
		for(Node n = hand; n != null; n = n.next) {
			if(n != hand) {
				n.draw(g, false);
			} else {
				n.draw(g, true);
			}
		}
	}
	
	class Node {
		int w = Yard.BLOCK_SIZE;
		int h = Yard.BLOCK_SIZE;
		int row, col;
		Direction dir;
		Node next = null;
		Node prev = null;
		
		Node(int row, int col, Direction dir){
			this.row = row;
			this.col = col;
			this.dir = dir;
		}
		void draw(Graphics g, boolean ifHand){
			Color color = g.getColor();
			if(!ifHand) {
				g.setColor(Color.BLACK);
				g.fillRect(row * Yard.BLOCK_SIZE, col * Yard.BLOCK_SIZE, w, h);
			} else {
				g.setColor(Color.RED);
				g.fillOval(row * Yard.BLOCK_SIZE, col * Yard.BLOCK_SIZE, w, h);
			}
			g.setColor(color);
		}
	}
	
	public void keyPressed(KeyEvent e){
		int key = e.getKeyCode();
		switch(key){
		case KeyEvent.VK_LEFT :
			if(hand.dir != Direction.R)
			hand.dir = Direction.L;
			break;
		case KeyEvent.VK_UP :
			if(hand.dir != Direction.D)
			hand.dir = Direction.U;
			break;
		case KeyEvent.VK_RIGHT :
			if(hand.dir != Direction.L)
			hand.dir = Direction.R;
			break;
		case KeyEvent.VK_DOWN :
			if(hand.dir != Direction.U)
			hand.dir = Direction.D;
			break;
		}
	}
}
